package cn.doctorpda.conference.ui.splash;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import cn.doctorpda.conference.MyApplication;
import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.SplashInfo;
import cn.doctorpda.conference.bean.UpdataInfo;
import cn.doctorpda.conference.event.IsNew;
import cn.doctorpda.conference.event.UpdateMsg;
import cn.doctorpda.conference.localstore.ClientInfo;
import cn.doctorpda.conference.localstore.UserInfo;
import cn.doctorpda.conference.okhttp.OkHttpUtils;
import cn.doctorpda.conference.okhttp.callback.FileCallBack;
import cn.doctorpda.conference.presenter.SplashBgPresenter;
import cn.doctorpda.conference.presenter.SplashPresenter;
import cn.doctorpda.conference.presenter.UpdateVersionPresenter;
import cn.doctorpda.conference.ui.BaseFragment;
import cn.doctorpda.conference.ui.MainActivity;
import cn.doctorpda.conference.ui.SwitchAccountActivity;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.utils.AESUtil;
import cn.doctorpda.conference.utils.AppUtils;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.view.callbackview.IBaseView;
import cn.doctorpda.conference.view.callbackview.ISplashView;
import cn.doctorpda.conference.view.callbackview.IUpdateView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/13.
 */
public class SplashFragment extends BaseFragment implements IBaseView<String>, ISplashView {

    private SplashPresenter presenter;
    private SplashBgPresenter splashBgPresenter;
    private long time;
    private ImageView imv_wel_bg;
    private boolean isClick = false;
    private Bundle extras;
    private int flag = -1;
    private Handler handlerToHome;
    private String username;
    private String password;


    public static SplashFragment newInstance(Bundle extras) {
        SplashFragment fragment = new SplashFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.extras = getArguments();
        if (extras != null) {
            username = extras.getString("username");
            password = extras.getString("password");
            password = AESUtil.decrypt(password, "doctorpda8888888", "");
            flag = extras.getInt("flag", -1);
        }

        Log.i("hongshi", username + "===" + password);


        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            if (UserInfo.getUserInfo() != null) {
                //用户已经登录，并且与参数中是同一个用户 那么进入相应的详情页
                L.d(username + password);
                if (TextUtils.equals(username, UserInfo.getUserInfo().getMOBILE())) {
                    toThird(extras, getActivity());
                } else {
                    Intent i = new Intent(getActivity(), SwitchAccountActivity.class);
                    i.putExtras(extras);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getActivity().startActivity(i);
                }
            } else {
                Intent i = new Intent(getActivity(), SwitchAccountActivity.class);
                i.putExtras(extras);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(i);
            }
        }else {
            Log.i("hongshi", "-------");
            toThird(extras, getActivity());
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_splash, container, false);
        imv_wel_bg = (ImageView) view.findViewById(R.id.splash_bg);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new SplashPresenter(this);
        splashBgPresenter = new SplashBgPresenter(this);
        time = System.currentTimeMillis();
        String clientId = ClientInfo.getClientId(getActivity());
        handlerToHome = new Handler();
        if (TextUtils.isEmpty(clientId)) {
            presenter.getClientId();
            splashBgPresenter.GetWel();
        } else {
            splashBgPresenter.GetWel();
        }


    }

    @Override
    public void setResult(Msg msg, String data) {
        ClientInfo.setClientId(getActivity(), data);
        splashBgPresenter.GetWel();
    }


    @Override
    public void setError(Call call, Exception e) {
        splashBgPresenter.GetWel();
    }

    @Override
    public String getPageName() {
        return "SplashFragment";
    }

    private void installAPK(String msg) {
        EventBus.getDefault().post(new UpdateMsg(msg));
        /*Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setDataAndType(Uri.parse(t), "application/vnd.android.package-archive");
        getActivity().startActivity(intent);*/
    }



    private void enterHome() {
            if (!isClick) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
    }

    /*欢迎图
    * */
    @Override
    public void welcomeResult(final SplashInfo info) {
        L.d("欢迎图url" + info.getContents().get(0).getThumb_pad());
//        ll_wel_bg.setBackground();
        ImageUtils.loadImageWel(getActivity(), info.getContents().get(0).getThumb_pad(), imv_wel_bg);
        imv_wel_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isClick = true;
//                T.showShort(getContext(),"ggg");
                EventBus.getDefault().post(new IsNew("isClick"));
                DetailActivity.startActivity(getActivity(), info.getContents().get(0).getTitle(), String.valueOf(info.getContents().get(0).getId()), "", info.getContents().get(0).getHref_url());
                getActivity().finish();
            }
        });


    }

    void toThird(Bundle bundle, Context context) {
        int flag=-1;
        if (bundle!=null) {
             flag = bundle.getInt("flag", -1);
        }
        Log.i("hongshi", flag+"===");

        if (flag == 2) {
            toMain(context);
        } else if (flag == 4) {
            //进入各种详情页面
            String id = bundle.getString("id");
            String type = bundle.getString("type");
            if (TextUtils.isEmpty(type)) {
                toMain(context);
                return;
            }
            if (TextUtils.equals(type, "con")) {
                //进入会议详情页
                String url = bundle.getString("url");
                if (TextUtils.isEmpty(url)) {
                    //url 为空 进入会议首页
                    toMain(context);
                } else {
                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra("id", id);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("type", type);
                    intent.putExtra("url", url);
                    intent.putExtra("flag", 1);
                    context.startActivity(intent);
                }
            }
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    enterHome();
                }
            }, 3000);
        }
    }

    void toMain(Context context) {
        Intent intent = new Intent(context, SplashActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public void splashError(Call call, Exception e) {

    }
}
